<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card v-loading="loading">
        <el-tabs>
          <el-tab-pane label="角色管理">
            <!-- layout布局 -->
            <el-row style="height: 60px">
              <el-button icon="el-icon-plus" size="small" type="primary" @click="showDialog = true">新增角色</el-button>
            </el-row>

            <!-- 表格 -->
            <!-- 
              data 显示的数据
              border 是否带有纵向边框
             -->
            <el-table
              :data="list"
              border>
              <el-table-column
                type="index"
                align="center"
                label="序号"
                width="120">
              </el-table-column>
              <!-- 
                type: 对应列的类型。如果设置了 selection 则显示多选框；
                如果设置了 index 则显示该行的索引（从 1 开始计算）；
                如果设置了 expand 则显示为一个可展开的按钮

                prop 对应列内容的字段名，也可以使用 property 属性
                align 对齐方式
                label 显示的标题
               -->
              <el-table-column
                prop="name"
                align="center"
                label="角色名"
                width="240">
              </el-table-column>
              <el-table-column
                prop="description"
                align="center"
                label="描述">
              </el-table-column>
              <el-table-column
                align="center"
                label="操作">
                <template slot-scope="{row}">
                  <el-button size="small" type="success" @click="allocation(row.id)">分配权限</el-button>
                  <el-button size="small" type="primary" @click="editRole(row.id)">编辑</el-button>
                  <el-button size="small" type="danger" @click="deleteRole(row.id)">删除</el-button>
                </template>
              </el-table-column>
            </el-table>
            <el-row type="flex" justify="center" align="middle" style="height: 60px">
              <!--
                el-pagination 分页组件 
                layout 组件布局，子组件名用逗号分隔
                layout="prev, pager, next," 不要加后面的逗号
                page-size 每页显示条目个数，支持 .sync 修饰符
                total 总条目数
                current-change currentPage 改变时会触发
               -->
              <el-pagination
                layout="prev, pager, next"
                :total="page.total"
                :page-size="page.pagesize"
                @current-change="changePage"  
              >
              </el-pagination>
            </el-row>
          </el-tab-pane>
          <!-- 右侧的公司信息 -->
          <el-tab-pane label="公司信息">
            <!-- 提示框 -->
            <el-alert
              title="对公司名称、公司地址、营业执照、公司地区的更新，将使得公司资料被重新审核，请谨慎修改"
              type="info"
              show-icon
              :closable="false"
            />

            <!-- from表单 -->
            <el-form label-width="120px" style="margin-top:50px">
              <el-form-item label="企业名称">
                <el-input v-model="formData.name" disabled style="width:400px"></el-input>
              </el-form-item>
              <el-form-item label="公司地址">
                <el-input v-model="formData.companyAddress" disabled style="width:400px"></el-input>
              </el-form-item>
              <el-form-item label="公司电话">
                <el-input v-model="formData.companyPhone" disabled style="width:400px"></el-input>
              </el-form-item>
              <el-form-item label="邮箱">
                <el-input v-model="formData.mailbox" disabled style="width:400px"></el-input>
              </el-form-item>
              <el-form-item label="备注">
                <el-input v-model="formData.remarks" type="textarea" :rows="3" disabled style="width:400px"></el-input>
              </el-form-item>
            </el-form>
          </el-tab-pane>
        </el-tabs>
      </el-card>

      <!-- 弹出层 -->
      <add-role ref="addRole" :showDialog.sync="showDialog" @getRoleList="getRoleList"/>
      <!-- 分配权限弹出框 -->
      <permission ref="permission" :showPermission.sync="showPermission"/>
    </div>
  </div>
</template>

<script>
import { getRoleList, getCompanyInfo, deleteRole } from '@/api/setting'
import addRole from './components/add-role'
import permission from './components/permission'
export default {
  name: 'setting',
  data() {
    return {
      list: [], // 承接数组
      page: {
        // 放置页码及相关数据
        page: 1, // 当前几页
        pagesize: 10, // 一页多少条数据
        total: 0, // 记录总数,
      },
      formData: {}, // 公司信息
      showDialog: false, // 控制弹出框的状态
      loading: false, // 加载的状态
      showPermission: false // 分配权限弹出框的状态
    }
  },
  created() {
    this.getRoleList()
    this.getCompanyInfo()
  },
  methods: {
    async getRoleList() {
      this.loading = true
      const { total, rows } = await getRoleList(this.page)
      this.page.total = total
      this.list = rows
      this.loading = false
    },
    async getCompanyInfo() {
      this.formData = await getCompanyInfo(this.$store.getters.companyId)
      this.formData.name = '深圳市腾讯计算机系统有限公司'
    },
    async deleteRole(id) {
      try {
        await this.$confirm('确认删除该角色吗?')
        await deleteRole(id)
        this.getRoleList()
        this.$message.success('删除角色成功')
      } catch (error) {
        console.log(error);
      }
    },
    changePage(newPage) {
      this.page.page = newPage
      this.getRoleList()
    },
    editRole(id) {
      this.$refs.addRole.getRoleDetail(id)
      this.showDialog = true // 打开弹出框
    },
    async allocation(id) {
      await this.$refs.permission.getRoleDetail(id)
      this.showPermission = true
    }
  },
  components: {
    addRole,
    permission
  }
}
</script>

<style>

</style>
